<template>
  <div class="edit-form"  >
    <el-dialog title="添加视频" :visible.sync="dialogFormVisible">
      <el-form v-model="editform" style="text-align: left" ref="dataForm">
        <el-form-item label="视频名" :label-width="formLabelWidth" prop="title">
          <el-input v-model="editform.videoName" autocomplete="off" placeholder="不加《》"></el-input>
        </el-form-item>
        <el-form-item label="视频" :label-width="formLabelWidth" prop="abs">
          <el-upload
                     :limit="1"
                     :file-list="formFileList"
                     :http-request="handleUploadForm"
                     :on-exceed="formHandleExceed"
                     :on-remove="formHandleRemove"
                     :before-upload="beforeUploadForm"
                     accept=".flv "
                     action=""
          >
            <el-button type="primary" style="margin-top: 2px">选择文件并上传</el-button>
            <span slot="tip" class="el-upload__tip" style="margin: 0 10px;">只能上传mp4文件，100M</span>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UpVideo",
  data() {
    return {
      dialogFormVisible: false,
      editform:{
        videoName:"",
        classId:"",
      },
      formLabelWidth: '120px',
      formFileList: [],
      uploadFormFileList: [],
      keyNum:0,
    }
  },
  methods:{
    setEdit(item){
      this.editform.classId=item.classId;
    },
    beforeUploadForm (file) {
      // 验证文件大小
      if (file.size / 1024 / 1024 >200 ) {
        this.$message({
          message: `上传文件大小不能超过100M!`,
          type: 'warning'
        })
        return false
      }
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'flv'
      if (!extension) {
        this.$message({
          message: '上传文件只能是flv格式!',
          type: 'warning'
        })
      }
      return extension
    },
    // 移除上传列表中文件
    formHandleRemove (file) {
      let thiz = this
      for (let i = 0; i < thiz.uploadFormFileList.length; i++) {
        if (thiz.uploadFormFileList[i].pname === file.name) {
          thiz.uploadFormFileList.splice(i, 1)
          break
        }
      }
    },
    // 允许上传文件个数验证
    formHandleExceed (files, formFileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + formFileList.length} 个文件`)
    },
    // 上传文件
    handleUploadForm (param) {
      let form = new FormData();
      form.append("file", param.file);
      form.append("name",this.editform.videoName)
      form.append("classId",this.editform.classId)
      if(this.editform.videoName==="") {
        this.$message('视频名未填写');
        return;
      }
      let loading = this.$loading({
        lock: true,
        text: '上传中，请稍候...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$axios.post('/uploadVideo',form).then(resp => {
        if(resp.data===200) {
          this.$message('上传文件成功，')
          this.formFileList = []
          this.uploadFormFileList = []
          this.reload();
          loading.close()
        }
        else{
          this.$message('上传文件失败，')
        }
      })

    },
    //局部刷新
    reload(){
      console.log(this.keyNum)
      this.keyNum++;
    },
    //

  }
}
</script>

<style scoped>

</style>

